package com.racetime.serviceImpl;

import com.racetime.dao.DateDao;
import com.racetime.daoImpl.DateDaoImpl;
import com.racetime.po.Date;
import com.racetime.service.DateService;
import com.racetime.util.DateUtil;

import java.util.List;

/**
 * @author : Ice'Clean
 * @date : 2021-05-24
 *
 * 日程服务实现类
 */
public class DateServiceImpl implements DateService {

    DateDao dateDao = new DateDaoImpl();

    @Override
    public List<Date> findDateByUserId(int userId) {
        return dateDao.findDateByUserId(userId);
    }

    @Override
    public Date createNewDate(int userId, String dateName, String dateContent, String dateClass, int dateLevel, String dateStart, String dateEnd) {
        int dateId = dateDao.createNewDate(new Date(userId, dateName, dateContent, dateClass, dateLevel, dateStart, dateEnd));
        return dateDao.findDateById(dateId);
    }

    @Override
    public boolean updateDate(int dateId, String dateName, String dateContent, String dateClass, int dateLevel, String dateStart, String dateEnd, String dateFinish) {
        // 获取指定日程对象
        Date date = dateDao.findDateById(dateId);

        // 修改日程对象数据
        date.setDateName(dateName);
        date.setDateContent(dateContent);
        date.setDateClass(dateClass);
        date.setDateLevel(dateLevel);
        date.setDateStart(dateStart);
        date.setDateEnd(dateEnd);
        date.setDateFinish(dateFinish);

        // 更新日程对象
        return dateDao.updateDate(date);
    }

    @Override
    public boolean isTimeClash(int userId, String startTime, String endTime) {
        return dateDao.findDateByTimeScope(userId, startTime, endTime).size() != 0;
    }

    @Override
    public Date getCurrentDate(int userId) {
        return dateDao.findDateByTime(userId, DateUtil.getTime());
    }

    @Override
    public boolean deleteDate(int dateId) {
        return dateDao.deleteDate(dateId);
    }
}
